<?php
	class EventModel extends CI_Model
	{
		public function __construct()
		{
			$this->load->database();
		}
		public final function addEvent($event)
		{
			$e = $event;
			$a = array
			(
				'Name' => $e->Name,
				'Type' => $e->Type,
				'Description' => $e->Description,
				'DateFrom' => $e->DateFrom,
				'DateTo' => $e->DateTo,
				'NgoId' => $e->NgoId
			);
			$this->db->insert('events', $a);
		}
		public final function getAllEvents()
		{
			$q = $this->db->get('events');
			return $q->result_object();
		}
		public final function getEventById($id)
		{
			$q = $this->db->get_where('events', array('Id' => $id));
			return $q->result_object();
		}
		public final function updateEvent($event)
		{
			$e = $event;
			$a = array
			(
				'Name' => $e->Name,
				'Type' => $e->Type,
				'Description' => $e->Description,
				'DateFrom' => $e->DateFrom,
				'DateTo' => $e->DateTo,
				'NgoId' => $e->NgoId
			);
			$this->db->where('Id', $e->Id);
			$this->db->update('events', $a);
		}
		public final function removeEventById($id)
		{
			$q = $this->db->delete('event', array('Id', $id));
			return $q->result_object();
		}
		public final function setAssignedBeneficiaries()
		{
			$p = $this->input->post;
			$this->db->trans_start();
			for($a = 0; $a < count($p('beneficiaries')); $a++)
			{
				//$beneficiaryId = $p('beneficiaries')[$a];
				$this->db->where('Id', $beneficiaryId);
				$data = array
				(
					'NgoId' => $p->NgoId,
					'EventId' => $p->EventId
				);
				$this->db->update('Beneficiaries', $data);
			}
			$this->db->trans_complete();
		}
		public final function getAssignedBeneficiaries($ngoId, $eventId)
		{
			$s = 'SELECT * FROM Beneficiaries WHERE NgoId = ? AND EventId = ?';
			$q = $this->db->query($s, array($ngoId, $eventId));
			return $q;
		}
	}